﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Excel = Microsoft.Office.Interop.Excel;

namespace 毕业设计__排课软件
{
    /// <summary>
    /// SS.xaml 的交互逻辑
    /// </summary>
    public partial class SS : Window
    {
        List<Lesson> list = new List<Lesson>();
        List<string> ls1 = new List<string>();
        List<string> ls2 = new List<string>();
        List<string> ls3 = new List<string>();
        List<string> ls4 = new List<string>();
        ArrayList array = new ArrayList();
        List<TeachingProject> _classInfo = new List<TeachingProject>();
        List<Teacher_Info_> teacherInfoList = new List<Teacher_Info_>();
        readonly string _dbPath;
        public SS()
        {
            InitializeComponent();

            #region 初始化界面




            list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            list.Add(new Lesson
            {
                lesson1 = "",
                lesson2 = "",
                lesson3 = "",
                lesson4 = "",
                lesson5 = "",
                week1 = "",
                week2 = "",
                week3 = "",
                week4 = "",
                week5 = "",
                didian1 = "",
                didian2 = "",
                didian3 = "",
                didian4 = "",
                didian5 = "",
                teacher1 = "",
                teacher2 = "",
                teacher3 = "",
                teacher4 = "",
                teacher5 = ""
            });
            var path0 = Environment.CurrentDirectory;
            var path1 = path0.Substring(0, path0.LastIndexOf("\\", StringComparison.Ordinal));
            var path2 = path1.Substring(0, path1.LastIndexOf("\\", StringComparison.Ordinal));
            _dbPath = path2 + "\\DB\\bs_pk.db";
            ls1.Add("软件工程    夏大伟");
            ls1.Add("计算机网络基础    李星华");
            ls1.Add("操作系统    李星华");
            ls2.Add("夏大伟");
            ls2.Add("李星华");
            ls2.Add("王作彤");
            ls3.Add("软件一班");
            ls3.Add("软件二班");
            ls3.Add("软件三班");
            ls4.Add("1308");
            ls4.Add("1309");
            ls4.Add("1310");
            //lb3.ItemsSource = ls4;
            //bjlb.ItemsSource = ls3;
            //lb2.ItemsSource = ls2;
            DgCurriculum.ItemsSource = list;
            //lb1.ItemsSource = ls1;
            //array.Add(2);//列表第一项：分类，1代表班级，2代表课程，3代表教师，4代表教室
            //array.Add("1");//列表第二项：传入对应数据，班级传入专业，课程传入课程名称，教师传入教师名称，教室传入教室名称
            //array.Add(1);//列表第三项：如果为班级分类，则需要传入班级小号
            //array.Add("0");//列表第四项：传入周数，若为所有周，则传入0
            //Display_CourseInformation(array);

            #endregion

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();
                using (new SQLiteCommand(conn))
                {
                    const string sql = "select major from Class_Info GROUP BY major";         //查询数据库当中的专业信息
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        CbProfessional.Items.Add(reader["major"].ToString());                 //将查询到的专业信息显示在前台combox上
                    }
                    CbProfessional.Items.Add("全部");                                         //在combox中添加全部选项卡
                }

                using (new SQLiteCommand(conn))
                {
                    const string sql = "select Grade from Class_Info GROUP BY Grade";
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        CbClass.Items.Add(reader["Grade"].ToString());
                    }
                }

                using (new SQLiteCommand(conn))
                {
                    const string sql = "SELECT * FROM Lesson_Info";
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        var tp = new TeachingProject
                        {

                            CourseName = reader["Course_Name"].ToString(),
                            CourseId = reader["Course_ID"].ToString(),
                            TotalHours = reader["Total_Class"].ToString(),
                            /*WeekOfTeaching = reader["Teaching_Week"].ToString(),*/
                            UseComputerRoom = reader["jifang_Num"].ToString(),
                            UseClassroomHours = reader["jiaoshi_Num"].ToString(),
                            UsePcClassroom = reader["qita_Num"].ToString(),
                        };
                        _classInfo.Add(tp);
                    }
                    LbCourse.ItemsSource = _classInfo;
                }

                using (new SQLiteCommand(conn))                                             //初始化教师信息
                {

                    const string sql = "select * from Teacher_Info";
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        var tp = new Teacher_Info_
                        {
                            Teacher_ID = reader["Teacher_ID"].ToString(),
                            Teacher = reader["Teacher_Name"].ToString()
                        };
                        teacherInfoList.Add(tp);
                    }

                    LbTeacherInfo.ItemsSource = teacherInfoList;
                }

            }

            CbWeek.Items.Add("全部");
            for (int j = 1; j < 20; j++)
            {
                if (j < 10)
                {
                    CbWeek.Items.Add('0' + j.ToString());
                    continue;
                }
                CbWeek.Items.Add(j.ToString());
            }
            LbClassInfo.SelectedIndex = 0;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)                //初始化窗口中分页控件内的教室信息
        {
            #region 数据库操作初始化分页控件内的教室信息
            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='多媒体'";
                        var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                        var reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            LbMulClassroom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='笔记本实验室'";
                        var command = new SQLiteCommand(sql, conn); //查询课程的详细信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            LbLapClassroom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='机房'";
                        var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            LbMacClasseoom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT Classroom_Name FROM Classroom_Info where Classroom_Type ='普通教室'";
                        var command = new SQLiteCommand(sql, conn);                 //查询课程的详细信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            LbComClasseoom.Items.Add(reader["Classroom_Name"]);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }
            #endregion
        }


        /// <summary>
        ///列表第一项：分类，1代表班级，2代表课程，3代表教师，4代表教室
        ///列表第二项：传入对应数据，班级传入专业，课程传入课程名称，教师传入教师名称，教室传入教室名称
        ///列表第三项：如果为班级分类，则需要传入班级小号
        ///列表第四项：传入周数，若为所有周，则传入0
        /// </summary>
        /// <param name="array"></param>
        public void Display_CourseInformation(ArrayList array)
        {

            list[0].didian1 = "";
            list[0].didian2 = "";
            list[0].didian3 = "";
            list[0].didian4 = "";
            list[0].didian5 = "";
            list[0].lesson1 = "";
            list[0].lesson2 = "";
            list[0].lesson3 = "";
            list[0].lesson4 = "";
            list[0].lesson5 = "";
            list[0].teacher1 = "";
            list[0].teacher2 = "";
            list[0].teacher3 = "";
            list[0].teacher4 = "";
            list[0].teacher5 = "";
            list[0].week1 = "";
            list[0].week2 = "";
            list[0].week3 = "";
            list[0].week4 = "";
            list[0].week5 = "";

            list[1].didian1 = "";
            list[1].didian2 = "";
            list[1].didian3 = "";
            list[1].didian4 = "";
            list[1].didian5 = "";
            list[1].lesson1 = "";
            list[1].lesson2 = "";
            list[1].lesson3 = "";
            list[1].lesson4 = "";
            list[1].lesson5 = "";
            list[1].teacher1 = "";
            list[1].teacher2 = "";
            list[1].teacher3 = "";
            list[1].teacher4 = "";
            list[1].teacher5 = "";
            list[1].week1 = "";
            list[1].week2 = "";
            list[1].week3 = "";
            list[1].week4 = "";
            list[1].week5 = "";

            list[2].didian1 = "";
            list[2].didian2 = "";
            list[2].didian3 = "";
            list[2].didian4 = "";
            list[2].didian5 = "";
            list[2].lesson1 = "";
            list[2].lesson2 = "";
            list[2].lesson3 = "";
            list[2].lesson4 = "";
            list[2].lesson5 = "";
            list[2].teacher1 = "";
            list[2].teacher2 = "";
            list[2].teacher3 = "";
            list[2].teacher4 = "";
            list[2].teacher5 = "";
            list[2].week1 = "";
            list[2].week2 = "";
            list[2].week3 = "";
            list[2].week4 = "";
            list[2].week5 = "";

            list[3].didian1 = "";
            list[3].didian2 = "";
            list[3].didian3 = "";
            list[3].didian4 = "";
            list[3].didian5 = "";
            list[3].lesson1 = "";
            list[3].lesson2 = "";
            list[3].lesson3 = "";
            list[3].lesson4 = "";
            list[3].lesson5 = "";
            list[3].teacher1 = "";
            list[3].teacher2 = "";
            list[3].teacher3 = "";
            list[3].teacher4 = "";
            list[3].teacher5 = "";
            list[3].week1 = "";
            list[3].week2 = "";
            list[3].week3 = "";
            list[3].week4 = "";
            list[3].week5 = "";

            list[4].didian1 = "";
            list[4].didian2 = "";
            list[4].didian3 = "";
            list[4].didian4 = "";
            list[4].didian5 = "";
            list[4].lesson1 = "";
            list[4].lesson2 = "";
            list[4].lesson3 = "";
            list[4].lesson4 = "";
            list[4].lesson5 = "";
            list[4].teacher1 = "";
            list[4].teacher2 = "";
            list[4].teacher3 = "";
            list[4].teacher4 = "";
            list[4].teacher5 = "";
            list[4].week1 = "";
            list[4].week2 = "";
            list[4].week3 = "";
            list[4].week4 = "";
            list[4].week5 = "";
            using (var conn1 = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn1.ConnectionString = connsb.ToString();
                conn1.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn1))
                {
                    var classId = LbClassInfo.SelectedItem as ClassInfo;
                    string sql1 = string.Format("select distinct Course_Name,Classroom_Name,Teacher_Name,Teaching_Time from Configuration_Info, Lesson_Info, Classroom_Info, Teacher_Info, Class_Info where Configuration_Info.Course_ID = Lesson_Info.Course_ID and Configuration_Info.Teacher_ID = Teacher_Info.Teacher_ID and Configuration_Info.Classroom_ID = Classroom_Info.Classroom_ID and Configuration_Info.Class_ID = Class_Info.Class_ID ");
                    if (Convert.ToInt32(array[0]) == 1)
                    {
                        if (Convert.ToInt32(array[3]) == 0)
                        {
                            sql1 += string.Format("and major = '{0}' and Class = '{1}' and Grade = {2} ", classId.Major, array[2].ToString(), classId.Grade);
                        }
                        else
                        {
                            sql1 += string.Format("and Teaching_Time like '{0}' and major = '{1}' and Class = '{2}' and Grade = {3} ", array[3].ToString() + "__", array[1].ToString(), array[2].ToString(), classId.Grade);
                        }
                    }
                    else if (Convert.ToInt32(array[0]) == 2)
                    {
                        if (Convert.ToInt32(array[3]) == 0)
                        {
                            sql1 += string.Format("and Lesson_Info.Course_ID = '{0}' ", array[1].ToString());
                        }
                        else
                        {
                            sql1 += string.Format("and Teaching_Time like '{0}' and Lesson_Info.Course_ID = '{1}' ", array[3].ToString() + "__", array[1].ToString());
                        }
                    }
                    else if (Convert.ToInt32(array[0]) == 3)
                    {
                        if (Convert.ToInt32(array[3]) == 0)
                        {
                            sql1 += string.Format("and Teacher_Info.Teacher_ID = '{0}' ", array[1].ToString());
                        }
                        else
                        {
                            sql1 += string.Format("and Teaching_Time like '{0}' and Teacher_Info.Teacher_ID = '{1}' ", array[3].ToString() + "__", array[1].ToString());
                        }
                    }
                    else if (Convert.ToInt32(array[0]) == 4)
                    {
                        if (Convert.ToInt32(array[3]) == 0)
                        {
                            sql1 += string.Format("and Classroom_Name = '{0}' ", array[1].ToString());
                        }
                        else
                        {
                            sql1 += string.Format("and Teaching_Time like '{0}' and Classroom_Name = '{1}' ", array[3].ToString() + "__", array[1].ToString());
                        }
                    }
                    sql1 += "order BY Teaching_Time ASC,Course_Name ASC";
                    //string sql1 = string.Format("select Course_Name,Classroom_Name,Teacher_Name,Teaching_Time from Configuration_Info, Lesson_Info, Classroom_Info, Teacher_Info, Class_Info where Configuration_Info.Course_ID = Lesson_Info.Course_ID and Configuration_Info.Teacher_ID = Teacher_Info.Teacher_ID and Configuration_Info.Classroom_ID = Classroom_Info.Classroom_ID and Configuration_Info.Class_ID = Class_Info.Class_ID and Course_Name = '快速建站' order BY Course_Name ASC,Teaching_Time ASC");
                    cmd.CommandText = sql1;
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(cmd);
                    DataSet data = new DataSet();
                    adapter.Fill(data);
                    DataTable table = data.Tables[0];
                    foreach (DataRow tr in table.Rows)
                    {
                        Lesson l = new Lesson();
                        if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 1)
                        {
                            l = list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian1, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString()) //暂使用教室匹配解决
                            {
                                //l.didian1 = tr["Classroom_Name"].ToString();
                                //l.lesson1 = tr["Course_Name"].ToString();
                                l.week1 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher1 = tr["Teacher_Name"].ToString();
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 != tr["Course_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 != "")
                            {
                                l.didian1 += "/" + tr["Classroom_Name"];
                                l.lesson1 += "/" + tr["Course_Name"];
                                l.week1 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher1 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian1 != tr["Classroom_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian1 != "" &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 == tr["Course_Name"].ToString())
                            {
                                l.didian1 += "/" + tr["Classroom_Name"];
                                l.lesson1 += "/" + tr["Course_Name"];
                                l.week1 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher1 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher1 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson1 == tr["Course_Name"].ToString()
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher1 != "")
                            {
                                //if (Convert.ToInt32(l.week1) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week1;
                                //    l.week1 = temp;
                                //}
                                //else
                                //{
                                l.didian1 = tr["Classroom_Name"].ToString();
                                l.lesson1 = tr["Course_Name"].ToString();
                                l.week1 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher1 = tr["Teacher_Name"].ToString();
                                //}
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian1 = tr["Classroom_Name"].ToString();
                            l.lesson1 = tr["Course_Name"].ToString();
                            l.week1 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher1 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 2)
                        {
                            l = list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian2, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian2 = tr["Classroom_Name"].ToString();
                                //l.lesson2 = tr["Course_Name"].ToString();
                                l.week2 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher2 = tr["Teacher_Name"].ToString();
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 != tr["Course_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 != "")
                            {
                                l.didian2 += "/" + tr["Classroom_Name"];
                                l.lesson2 += "/" + tr["Course_Name"];
                                l.week2 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher2 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian2 != tr["Classroom_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian2 != "" &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 == tr["Course_Name"].ToString())
                            {
                                l.didian2 += "/" + tr["Classroom_Name"];
                                l.lesson2 += "/" + tr["Course_Name"];
                                l.week2 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher2 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher2 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson2 == tr["Course_Name"].ToString()
                                   list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher2 != "")
                            {
                                //if (Convert.ToInt32(l.week2) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week2;
                                //    l.week2 = temp;
                                //}
                                //else
                                //{
                                l.didian2 = tr["Classroom_Name"].ToString();
                                l.lesson2 = tr["Course_Name"].ToString();
                                l.week2 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher2 = tr["Teacher_Name"].ToString();
                                //}
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian2 = tr["Classroom_Name"].ToString();
                            l.lesson2 = tr["Course_Name"].ToString();
                            l.week2 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher2 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 3)
                        {
                            l = list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian3, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian3 = tr["Classroom_Name"].ToString();
                                //l.lesson3 = tr["Course_Name"].ToString();
                                l.week3 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher3 = tr["Teacher_Name"].ToString();
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 != tr["Course_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 != "")
                            {
                                l.didian3 += "/" + tr["Classroom_Name"];
                                l.lesson3 += "/" + tr["Course_Name"];
                                l.week3 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher3 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian3 != tr["Classroom_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian3 != "" &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 == tr["Course_Name"].ToString())
                            {
                                l.didian3 += "/" + tr["Classroom_Name"];
                                l.lesson3 += "/" + tr["Course_Name"];
                                l.week3 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher3 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher3 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson3 == tr["Course_Name"].ToString()
                                   list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher3 != "")
                            {
                                //if (Convert.ToInt32(l.week3) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week3;
                                //    l.week3 = temp;
                                //}
                                //else
                                //{
                                l.didian3 = tr["Classroom_Name"].ToString();
                                l.lesson3 = tr["Course_Name"].ToString();
                                l.week3 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher3 = tr["Teacher_Name"].ToString();
                                //}
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian3 = tr["Classroom_Name"].ToString();
                            l.lesson3 = tr["Course_Name"].ToString();
                            l.week3 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher3 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 4)
                        {
                            l = list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian4, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian4 = tr["Classroom_Name"].ToString();
                                //l.lesson4 = tr["Course_Name"].ToString();
                                l.week4 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                //l.teacher4 = tr["Teacher_Name"].ToString();
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 != tr["Course_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 != "")
                            {
                                l.didian4 += "/" + tr["Classroom_Name"];
                                l.lesson4 += "/" + tr["Course_Name"];
                                l.week4 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher4 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian4 != tr["Classroom_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian4 != "" &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 == tr["Course_Name"].ToString())
                            {
                                l.didian4 += "/" + tr["Classroom_Name"];
                                l.lesson4 += "/" + tr["Course_Name"];
                                l.week4 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher4 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher4 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson4 == tr["Course_Name"].ToString()
                                   list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher4 != "")
                            {
                                //if (Convert.ToInt32(l.week4) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week4;
                                //    l.week4 = temp;
                                //}
                                //else
                                //{
                                l.didian4 = tr["Classroom_Name"].ToString();
                                l.lesson4 = tr["Course_Name"].ToString();
                                l.week4 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher4 = tr["Teacher_Name"].ToString();
                                //}
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian4 = tr["Classroom_Name"].ToString();
                            l.lesson4 = tr["Course_Name"].ToString();
                            l.week4 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher4 = tr["Teacher_Name"].ToString();
                        }
                        else if (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10 == 5)
                        {
                            l = list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1];
                            string pat = string.Format(@"{0}$", tr["Classroom_Name"]);
                            Match match1 = Regex.Match(list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian5, pat);
                            if (match1.Value == tr["Classroom_Name"].ToString())
                            {
                                //l.didian5 = tr["Classroom_Name"].ToString();
                                //l.lesson5 = tr["Course_Name"].ToString();
                                l.week5 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //l.teacher5 = tr["Teacher_Name"].ToString();
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 != tr["Course_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 != "")
                            {
                                l.didian5 += "/" + tr["Classroom_Name"];
                                l.lesson5 += "/" + tr["Course_Name"];
                                l.week5 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher5 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian5 != tr["Classroom_Name"].ToString() &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].didian5 != "" &&
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 == tr["Course_Name"].ToString())
                            {
                                l.didian5 += "/" + tr["Classroom_Name"];
                                l.lesson5 += "/" + tr["Course_Name"];
                                l.week5 += "/" + (Convert.ToInt32(tr["Teaching_Time"]) / 100);
                                l.teacher5 += "/" + tr["Teacher_Name"];
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            if (/*_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].week1 == (Convert.ToInt32(tr["Teaching_Time"]) % 100 / 10).ToString() &&*/
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher5 == tr["Teacher_Name"].ToString() &&
                                //_list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].lesson5 == tr["Course_Name"].ToString()
                                   list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1].teacher5 != "")
                            {
                                //if (Convert.ToInt32(l.week5) > (Convert.ToInt32(tr["Teaching_Time"]) / 100))
                                //{
                                //    string temp = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                //    temp += "," + l.week5;
                                //    l.week5 = temp;
                                //}
                                //else
                                //{
                                l.didian5 = tr["Classroom_Name"].ToString();
                                l.lesson5 = tr["Course_Name"].ToString();
                                l.week5 += "," + (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                                l.teacher5 = tr["Teacher_Name"].ToString();
                                //}
                                list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                                continue;
                            }
                            l.didian5 = tr["Classroom_Name"].ToString();
                            l.lesson5 = tr["Course_Name"].ToString();
                            l.week5 = (Convert.ToInt32(tr["Teaching_Time"]) / 100).ToString();
                            l.teacher5 = tr["Teacher_Name"].ToString();
                        }
                        list[(Convert.ToInt32(tr["Teaching_Time"]) % 10) - 1] = l;
                    }
                }
            }

            for (int i = 0; i < 5; i++)
            {
                list[i].week1 = strChange(list[i].week1);
                list[i].week2 = strChange(list[i].week2);
                list[i].week3 = strChange(list[i].week3);
                list[i].week4 = strChange(list[i].week4);
                list[i].week5 = strChange(list[i].week5);

            }
            DgCurriculum.ItemsSource = null;
            DgCurriculum.ItemsSource = list;
        }

        int i = 0;
        private void DG_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (i == 0)
            {
                e.Row.Header = "    一节\n8:20-9:05\n    二节\n9:15-10:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                i++;
            }
            else if (i == 1)
            {
                e.Row.Header = "    三节\n10:20-11:05\n    四节\n11:15-12:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                i++;
            }
            else if (i == 2)
            {
                e.Row.Header = "    五节\n13:30-14:15\n    六节\n14:25-15:10";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                i++;
            }
            else if (i == 3)
            {
                e.Row.Header = "    七节\n15:30-16:15\n    八节\n16:25-17:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                i++;
            }
            else if (i == 4)
            {
                e.Row.Header = "    九节\n18:20-19:05\n    十节\n19:15-20:00";
                e.Row.MinHeight = 125;
                e.Row.MaxHeight = 125;
                i++;
                //DataGridCell dgc = DataGridPlus.GetCell(DG, 4, 1);
            }
            else if (i % 5 == 0)
            {
                e.Row.Header = "    一节\n8:20-9:05\n    二节\n9:15-10:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                i++;
            }
            else if (i % 5 == 1)
            {
                e.Row.Header = "    三节\n10:20-11:05\n    四节\n11:15-12:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                i++;
            }
            else if (i % 5 == 2)
            {
                e.Row.Header = "    五节\n13:30-14:15\n    六节\n14:25-15:10";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                i++;
            }
            else if (i % 5 == 3)
            {
                e.Row.Header = "    七节\n15:30-16:15\n    八节\n16:25-17:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                i++;
            }
            else if (i % 5 == 4)
            {
                e.Row.Header = "    九节\n18:20-19:05\n    十节\n19:15-20:00";
                e.Row.MinHeight = ActualHeight / 5 - 13;
                e.Row.MaxHeight = ActualHeight / 5 - 13;
                i++;
                //DataGridCell dgc = DataGridPlus.GetCell(DG, 4, 1);
            }
        }

        private void ZYCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<ClassInfo> classNum = new List<ClassInfo>();
            #region  数据库操作
            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();

                try
                {
                    using (new SQLiteCommand(conn))
                    {
                        string sql = "SELECT * FROM Class_Info where Grade ='" + CbClass.SelectedValue + "' and major='" + CbProfessional.SelectedValue + "'";
                        if (CbProfessional.SelectedValue.ToString() == "全部")                                //如果选择全部的话就显示出当前年级全部的班级
                        {
                            sql = "SELECT * FROM Class_Info where Grade ='" + CbClass.SelectedValue + "'";
                        }
                        var command = new SQLiteCommand(sql, conn);                                           //查询班级需要信息
                        var reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            ClassInfo item = new ClassInfo();
                            item.Grade = reader["Grade"].ToString();
                            item.Major = reader["major"].ToString();
                            item.Class = reader["Class"].ToString();
                            item.ClassNum = reader["Class_Num"].ToString();
                            item.ClassAllInfo = reader["Grade"].ToString() + reader["major"] + reader["Class"];
                            classNum.Add(item);//将年级专业班级信息拼接成一个完整班级信息显示在前台listbox当中
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored 
                }
            }
            #endregion
            LbClassInfo.ItemsSource = classNum;
        }
        private void LbClassInfo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //调用函数

            //列表第一项：分类，1代表班级，2代表课程，3代表教师，4代表教室
            //列表第二项：传入对应数据，班级传入班级ID，课程传入课程名称，教师传入教师名称，教室传入教室名称
            //列表第三项：如果为班级分类，则需要传入班级小号
            //列表第四项：传入周数，若为所有周，则传入0
            ArrayList arrayList = new ArrayList();
            arrayList.Add(1);
            var classInfo = LbClassInfo.SelectedItem as ClassInfo;
            if (classInfo != null)
            {
                arrayList.Add(classInfo.Grade + classInfo.Major);

                arrayList.Add(classInfo.Class);
            }
            else
            {
                return;
            }

            if (CbWeek.SelectedValue.ToString() == "全部")
            {
                arrayList.Add("0");
            }
            else
            {


                arrayList.Add(CbWeek.SelectedValue);

            }


            Display_CourseInformation(arrayList);


        }

        private void LbMulClassroom_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //调用函数

            //列表第一项：分类，1代表班级，2代表课程，3代表教师，4代表教室
            //列表第二项：传入对应数据，班级传入专业，课程传入课程名称，教师传入教师名称，教室传入教室名称
            //列表第三项：如果为班级分类，则需要传入班级小号
            //列表第四项：传入周数，若为所有周，则传入0
            string classId = "";
            try
            {
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:多媒体教室 Content:")
                {
                    classId = LbMulClassroom.SelectedValue.ToString();
                }
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:笔记本教室 Content:")
                {
                    classId = LbLapClassroom.SelectedValue.ToString();
                }
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:机房教室 Content:")
                {
                    classId = LbMacClasseoom.SelectedValue.ToString();
                }
                if (TcClassInfo.SelectedItem.ToString() == "System.Windows.Controls.TabItem Header:普通教室 Content:")
                {
                    classId = LbComClasseoom.SelectedValue.ToString();
                }
            }
            catch (Exception)
            {
                //
            }

            ArrayList arrayList = new ArrayList();
            arrayList.Add(4);
            arrayList.Add(classId);

            arrayList.Add(0);

            if (CbWeek.SelectedValue.ToString() == "全部")
            {
                arrayList.Add("0");
            }
            else
            {

                arrayList.Add(CbWeek.SelectedValue);

            }


            Display_CourseInformation(arrayList);

        }
        private void LbTeacherInfo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //调用函数

            //列表第一项：分类，1代表班级，2代表课程，3代表教师，4代表教室
            //列表第二项：传入对应数据，班级传入专业，课程传入课程名称，教师传入教师名称，教室传入教室名称
            //列表第三项：如果为班级分类，则需要传入班级小号
            //列表第四项：传入周数，若为所有周，则传入0
            ArrayList arrayList = new ArrayList();
            arrayList.Add(3);

            var TeacherInfo = LbTeacherInfo.SelectedItem as Teacher_Info_;
            arrayList.Add(TeacherInfo.Teacher_ID);
            arrayList.Add(0);

            if (CbWeek.SelectedValue.ToString() == "全部")
            {
                arrayList.Add("0");
            }
            else
            {
                arrayList.Add(CbWeek.SelectedValue);

            }


            Display_CourseInformation(arrayList);

        }

        private void lb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //调用函数

            //列表第一项：分类，1代表班级，2代表课程，3代表教师，4代表教室
            //列表第二项：传入对应数据，班级传入专业，课程传入课程名称，教师传入教师名称，教室传入教室名称
            //列表第三项：如果为班级分类，则需要传入班级小号
            //列表第四项：传入周数，若为所有周，则传入0
            ArrayList arrayList = new ArrayList();
            arrayList.Add(2);

            var TeachingInfo = LbCourse.SelectedItem as TeachingProject;
            //arrayList[1] = TeachingInfo.CourseId;
            arrayList.Add(TeachingInfo.CourseId);
            arrayList.Add(0);

            if (CbWeek.SelectedValue.ToString() == "全部")
            {
                arrayList.Add("0");
            }
            else
            {
                arrayList.Add(CbWeek.SelectedValue);

            }


            Display_CourseInformation(arrayList);
        }

        string strChange(string str)
        {
            if (str == "")
            {
                return "";
            }
            if (str.Length <= 2)
            {
                return str;
            }

            List<string> aList1 = new List<string>();
            int i;

            if (str.Substring(1, 1) == "," || str.Substring(1, 1) == "/")
            {
                aList1.Add(str.Substring(0, 1));
                i = 2;
            }
            else
            {
                aList1.Add(str.Substring(0, 1) + str.Substring(1, 1));
                i = 3;
            }

            for (; i < str.Length; i++)
            {
                if (str.Substring(i, 1) == "," || str.Substring(i, 1) == "/")
                {

                    if (str.Substring(i - 1, 1) != "," && str.Substring(i - 1, 1) != "/")
                    {
                        if (str.Substring(i - 2, 1) != "," && str.Substring(i - 2, 1) != "/")
                        {
                            aList1.Add(str.Substring(i - 2, 1) + str.Substring(i - 1, 1));
                        }
                        else
                        {
                            aList1.Add(str.Substring(i - 1, 1));
                        }
                    }

                    if (str.Substring(i, 1) == "/")
                    {
                        aList1.Add(str.Substring(i, 1));
                    }
                }
            }

            if (str.Substring(str.Length - 2, 1) == "," || str.Substring(str.Length - 2, 1) == "/")
            {
                aList1.Add(str.Substring(str.Length - 1, 1));

            }
            else
            {
                aList1.Add(str.Substring(str.Length - 2, 1) + str.Substring(str.Length - 1, 1));

            }

            for (i = 0; i < aList1.Count - 1; i++)
            {
                if (aList1[i] != "/" && aList1[i + 1] != "/" && aList1[i] != "," && aList1[i + 1] != ",")
                {
                    if (Convert.ToInt32(aList1[i]) + 1 != Convert.ToInt32(aList1[i + 1]))
                    {
                        aList1.Insert(i + 1, ",");
                    }
                }
            }
            string res = aList1[0];
            for (i = 1; i < aList1.Count; i++)
            {
                if (aList1[i] == ",")
                {
                    res = res + "-" + aList1[i - 1] + ",";

                }
                if (aList1[i - 1] == ",")
                {
                    res = res + aList1[i];
                }
                if (aList1[i] == "/")
                {
                    res = res + "-" + aList1[i - 1] + "/";

                }
                if (aList1[i - 1] == "/")
                {
                    res = res + aList1[i];
                }
            }

            res = res + "-" + aList1[aList1.Count - 1];

            return res;

            //字符串变成list，每个格子存放数字，去掉逗号

            //遍历数字，两个相邻的数字而且不相差一就加入逗号

            //遍历字符串拼接出结果

        }

        private void bt1_Click(object sender, RoutedEventArgs e)
        {
            List<string> classNameList = new List<string>();

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();
                using (new SQLiteCommand(conn))
                {
                    string sql = "SELECT Course_Name FROM Lesson_Info WHERE Course_Name like '%" + tb1.Text + "%'";
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        classNameList.Add(reader["Course_Name"].ToString());
                        //CbProfessional.Items.Add(reader["major"].ToString());                
                    }

                }


            }


            //var TeachingInfo = LbCourse.SelectedItem as TeachingProject;
            List<TeachingProject> classInfo = new List<TeachingProject>();

            foreach (var item1 in classNameList)
            {
                foreach (var item2 in LbCourse.Items)
                {
                    var TeachingInfo = item2 as TeachingProject;
                    if (item1 == TeachingInfo.CourseName)
                    {
                        classInfo.Add(TeachingInfo);
                    }
                }
            }
            var tpi = new TeachingProject
            {
                CourseName = "--------分割线----------"
            };
            classInfo.Add(tpi);

            foreach (var item in _classInfo)
            {
                classInfo.Add(item);
            }
            LbCourse.ItemsSource = null;
            LbCourse.ItemsSource = classInfo;

        }

        private void Taecher_click(object sender, RoutedEventArgs e)
        {
            List<string> teacherNameList = new List<string>();

            using (var conn = new SQLiteConnection())
            {
                var connsb = new SQLiteConnectionStringBuilder { DataSource = _dbPath };
                conn.ConnectionString = connsb.ToString();
                conn.Open();
                using (new SQLiteCommand(conn))
                {
                    string sql = "SELECT Teacher_Name FROM Teacher_Info WHERE Teacher_Name like '%" + tb2.Text + "%'";
                    var command = new SQLiteCommand(sql, conn);
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        teacherNameList.Add(reader["Teacher_Name"].ToString());
                        //CbProfessional.Items.Add(reader["major"].ToString());                
                    }

                }


            }


            //var TeachingInfo = LbCourse.SelectedItem as TeachingProject;
            List<Teacher_Info_> TeacherInfo = new List<Teacher_Info_>();

            foreach (var item1 in teacherNameList)
            {
                foreach (var item2 in LbTeacherInfo.Items)
                {
                    var TeachingInfo = item2 as Teacher_Info_;
                    if (item1 == TeachingInfo.Teacher)
                    {
                        TeacherInfo.Add(TeachingInfo);
                    }
                }
            }
            var tpi = new Teacher_Info_
            {
                Teacher = "--------分割线----------"
            };
            TeacherInfo.Add(tpi);

            foreach (var item in teacherInfoList)
            {
                TeacherInfo.Add(item);
            }
            LbTeacherInfo.ItemsSource = null;
            LbTeacherInfo.ItemsSource = TeacherInfo;
        }

        public void Export(DataGrid dataGrid, string excelTitle)
        {
            //for (int i = 0; i < dataGrid.Columns.Count; i++)
            //{
            //    if (dataGrid.Columns[i].Visibility == System.Windows.Visibility.Visible)//只导出可见列  
            //    {
            //        dt.Columns.Add(dataGrid.Columns[i].Header.ToString());//构建表头  
            //    }
            //}

            //for (int i = 0; i < dataGrid.Items.Count; i++)
            //{
            //    int columnsIndex = 0;
            //    System.Data.DataRow row = dt.NewRow();
            //    for (int j = 0; j < dataGrid.Columns.Count; j++)
            //    {
            //        if (dataGrid.Columns[j].Visibility == System.Windows.Visibility.Visible)
            //        {
            //            if (dataGrid.Items[i] != null && (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock) != null)//填充可见列数据  
            //            {
            //                row[columnsIndex] = (dataGrid.Columns[j].GetCellContent(dataGrid.Items[i]) as TextBlock).Text.ToString();
            //            }
            //            else row[columnsIndex] = "";

            //            columnsIndex++;
            //        }
            //    }
            //    dt.Rows.Add(row);
            //}
            DataTable dt = new DataTable();
            for (int i = 0; i < 6; i++)
            {
                if (i == 0)
                {
                    dt.Columns.Add("课程安排");
                }
                else if (i == 1)
                {
                    dt.Columns.Add("星期一");
                }
                else if (i == 2)
                {
                    dt.Columns.Add("星期二");
                }
                else if (i == 3)
                {
                    dt.Columns.Add("星期三");
                }
                else if (i == 4)
                {
                    dt.Columns.Add("星期四");
                }
                else if (i == 5)
                {
                    dt.Columns.Add("星期五");
                }
            }
            object[] values = new object[5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = list[i].lesson1;
                    }
                    else if (j == 1)
                    {
                        values[j] = list[i].lesson2;
                    }
                    else if (j == 2)
                    {
                        values[j] = list[i].lesson3;
                    }
                    else if (j == 3)
                    {
                        values[j] = list[i].lesson4;
                    }
                    else if (j == 4)
                    {
                        values[j] = list[i].lesson5;
                    }
                }
                dt.Rows.Add(values);
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = list[i].week1;
                    }
                    else if (j == 1)
                    {
                        values[j] = list[i].week2;
                    }
                    else if (j == 2)
                    {
                        values[j] = list[i].week3;
                    }
                    else if (j == 3)
                    {
                        values[j] = list[i].week4;
                    }
                    else if (j == 4)
                    {
                        values[j] = list[i].week5;
                    }
                }
                dt.Rows.Add(values);
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = list[i].didian1;
                    }
                    else if (j == 1)
                    {
                        values[j] = list[i].didian2;
                    }
                    else if (j == 2)
                    {
                        values[j] = list[i].didian3;
                    }
                    else if (j == 3)
                    {
                        values[j] = list[i].didian4;
                    }
                    else if (j == 4)
                    {
                        values[j] = list[i].didian5;
                    }
                }
                dt.Rows.Add(values);
                for (int j = 0; j < 5; j++)
                {
                    if (j == 0)
                    {
                        values[j] = list[i].teacher1;
                    }
                    else if (j == 1)
                    {
                        values[j] = list[i].teacher2;
                    }
                    else if (j == 2)
                    {
                        values[j] = list[i].teacher3;
                    }
                    else if (j == 3)
                    {
                        values[j] = list[i].teacher4;
                    }
                    else if (j == 4)
                    {
                        values[j] = list[i].teacher5;
                    }
                }
                dt.Rows.Add(values);
            }
            string FileName = ExcelExport(dt, excelTitle);
        }

        public string ExcelExport(DataTable DT, string title)
        {
            try
            {
                //创建Excel
                Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook ExcelBook = ExcelApp.Workbooks.Add(System.Type.Missing);
                //创建工作表（即Excel里的子表sheet） 1表示在子表sheet1里进行数据导出
                Microsoft.Office.Interop.Excel.Worksheet ExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelBook.Worksheets[1];

                //如果数据中存在数字类型 可以让它变文本格式显示
                ExcelSheet.Cells.NumberFormat = "@";

                //设置工作表名
                ExcelSheet.Name = "Sheet1";

                //设置Sheet标题
                string start = "A1";
                string end = ChangeASC(DT.Columns.Count) + "1";

                Excel.Range _Range = ExcelSheet.get_Range(start, "G1");
                _Range.Merge(0);    //单元格合并动作(要配合上面的get_Range()进行设计)
                _Range = (Microsoft.Office.Interop.Excel.Range)ExcelSheet.get_Range(start, end);
                _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                _Range.Font.Size = 18; //设置字体大小
                _Range.Font.Name = "黑体"; //设置字体的种类 
                //ExcelSheet.Cells[1, 1] = title;    //Excel单元格赋值
                _Range.EntireColumn.AutoFit(); //自动调整列宽

                Excel.Range r1 = ExcelSheet.get_Range("A3", "A6");
                r1.Merge(0);
                r1.RowHeight = 26;
                r1.ColumnWidth = 12;
                Excel.Range r2 = ExcelSheet.get_Range("A7", "A10");
                r2.Merge(0);
                r2.RowHeight = 26;
                Excel.Range r3 = ExcelSheet.get_Range("A11", "A14");
                r3.Merge(0);
                r3.RowHeight = 26;
                Excel.Range r4 = ExcelSheet.get_Range("A15", "A18");
                r4.Merge(0);
                r4.RowHeight = 26;
                Excel.Range r5 = ExcelSheet.get_Range("A19", "A22");
                r5.Merge(0);
                r5.RowHeight = 26;
                ExcelSheet.Cells[3, 1] = "一节\n8:20-9:05\n 二节\n9:15-10:00";
                ExcelSheet.Cells[7, 1] = "三节\n10:20-11:05\n四节\n11:15-12:00";
                ExcelSheet.Cells[11, 1] = "五节\n13:30-14:15\n六节\n14:25-15:10";
                ExcelSheet.Cells[15, 1] = "七节\n15:30-16:15\n八节\n16:25-17:00";
                ExcelSheet.Cells[19, 1] = "九节\n18:20-19:05\n十节\n19:15-20:00";
                ExcelSheet.Cells[3, 2] = "课程";
                ExcelSheet.Cells[4, 2] = "教学周";
                ExcelSheet.Cells[5, 2] = "地点";
                ExcelSheet.Cells[6, 2] = "教师";
                ExcelSheet.Cells[7, 2] = "课程";
                ExcelSheet.Cells[8, 2] = "教学周";
                ExcelSheet.Cells[9, 2] = "地点";
                ExcelSheet.Cells[10, 2] = "教师";
                ExcelSheet.Cells[11, 2] = "课程";
                ExcelSheet.Cells[12, 2] = "教学周";
                ExcelSheet.Cells[13, 2] = "地点";
                ExcelSheet.Cells[14, 2] = "教师";
                ExcelSheet.Cells[15, 2] = "课程";
                ExcelSheet.Cells[16, 2] = "教学周";
                ExcelSheet.Cells[17, 2] = "地点";
                ExcelSheet.Cells[18, 2] = "教师";
                ExcelSheet.Cells[19, 2] = "课程";
                ExcelSheet.Cells[20, 2] = "教学周";
                ExcelSheet.Cells[21, 2] = "地点";
                ExcelSheet.Cells[22, 2] = "教师";

                //写表头
                for (int m = 0; m < DT.Columns.Count; m++)
                {
                    ExcelSheet.Cells[2, m + 2] = DT.Columns[m].ColumnName.ToString();

                    start = "B2";
                    end = ChangeASC(DT.Columns.Count) + "2";

                    _Range = ExcelSheet.get_Range(start, "G2");
                    _Range.Font.Size = 13; //设置字体大小
                    _Range.Font.Name = "黑体"; //设置字体的种类  
                    _Range.ColumnWidth = 20; //自动调整列宽 
                    _Range.RowHeight = 18;
                    _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                }

                //写数据
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    for (int j = 0; j < DT.Columns.Count; j++)
                    {
                        //Excel单元格第一个从索引1开始
                        // if (j == 0) j = 1;
                        ExcelSheet.Cells[i + 3, j + 3] = DT.Rows[i][j].ToString();
                    }
                }

                //表格属性设置
                for (int n = 0; n < DT.Rows.Count + 1; n++)
                {
                    start = "A" + (n + 3).ToString();
                    end = ChangeASC(DT.Columns.Count) + (n + 3).ToString();

                    //获取Excel多个单元格区域
                    _Range = ExcelSheet.get_Range(start, end);

                    _Range.Font.Size = 12; //设置字体大小
                    _Range.Font.Name = "宋体"; //设置字体的种类
                    _Range.WrapText = true;

                    //_Range.EntireColumn.AutoFit(); //自动调整列宽
                    _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式 _Range.EntireColumn.AutoFit(); //自动调整列宽 
                }

                for (int i = 0; i < 2; i++)
                {
                    //获取Excel多个单元格区域
                    _Range = ExcelSheet.get_Range("A1", "G22");
                    _Range.Borders.LineStyle = 1;
                }

                ExcelApp.DisplayAlerts = false; //保存Excel的时候，不弹出是否保存的窗口直接进行保存 

                //弹出保存对话框,并保存文件
                Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
                sfd.DefaultExt = ".xlsx";
                sfd.Filter = "Office 2010 File|*.xlsx|Office 2010 File|*.xls|所有文件|*.*";
                if (sfd.ShowDialog() == true)
                {
                    if (sfd.FileName != "")
                    {
                        ExcelBook.SaveAs(sfd.FileName);  //将其进行保存到指定的路径
                        MessageBox.Show("导出文件存储为: " + sfd.FileName);
                    }
                }

                //释放可能还没释放的进程
                ExcelBook.Close();
                ExcelApp.Quit();
                //PubHelper.Instance.KillAllExcel(ExcelApp);

                return sfd.FileName;
            }
            catch
            {
                MessageBox.Show("导出文件保存失败！");
                return null;
            }
        }

        /// <summary>
        /// 获取当前列列名,并得到EXCEL中对应的列
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string ChangeASC(int count)
        {
            string ascstr = "";

            switch (count)
            {
                case 1:
                    ascstr = "A";
                    break;
                case 2:
                    ascstr = "B";
                    break;
                case 3:
                    ascstr = "C";
                    break;
                case 4:
                    ascstr = "D";
                    break;
                case 5:
                    ascstr = "E";
                    break;
                case 6:
                    ascstr = "F";
                    break;
                case 7:
                    ascstr = "G";
                    break;
                case 8:
                    ascstr = "H";
                    break;
                case 9:
                    ascstr = "I";
                    break;
                case 10:
                    ascstr = "J";
                    break;
                case 11:
                    ascstr = "K";
                    break;
                case 12:
                    ascstr = "L";
                    break;
                case 13:
                    ascstr = "M";
                    break;
                case 14:
                    ascstr = "N";
                    break;
                case 15:
                    ascstr = "O";
                    break;
                case 16:
                    ascstr = "P";
                    break;
                case 17:
                    ascstr = "Q";
                    break;
                case 18:
                    ascstr = "R";
                    break;
                case 19:
                    ascstr = "S";
                    break;
                case 20:
                    ascstr = "Y";
                    break;
                default:
                    ascstr = "U";
                    break;
            }
            return ascstr;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var classId = LbClassInfo.SelectedItem as ClassInfo;
            Export(DgCurriculum, classId.Major.ToString() + classId.Class.ToString() + "班");
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                g1.Height = ActualHeight;
                g1.Width = ActualWidth - 350;
                LbTeacherInfo.Height = ActualHeight - 560;
                foreach (var item in list)
                {
                    if (item.didian1 == "")
                    {
                        item.didian1 = " ";
                    }
                    else if (item.didian2 == "")
                    {
                        item.didian2 = " ";
                    }
                    else if (item.didian3 == "")
                    {
                        item.didian3 = " ";
                    }
                    else if (item.didian4 == "")
                    {
                        item.didian4 = " ";
                    }
                    else if (item.didian5 == "")
                    {
                        item.didian5 = " ";
                    }
                }
                DgCurriculum.ItemsSource = null;
                DgCurriculum.ItemsSource = list;
            }
            if (WindowState == WindowState.Normal)
            {
                g1.Height = 689;
                g1.Width = 942;
                LbTeacherInfo.Height = 160;
                if (W1.Width != 1300)
                {
                    W1.Width = 1300;
                }
                if (W1.Height != 720)
                {
                    W1.Height = 720;
                }
                foreach (var item in list)
                {
                    if (item.didian1 == " ")
                    {
                        item.didian1 = "";
                    }
                    else if (item.didian2 == " ")
                    {
                        item.didian2 = "";
                    }
                    else if (item.didian3 == " ")
                    {
                        item.didian3 = "";
                    }
                    else if (item.didian4 == " ")
                    {
                        item.didian4 = "";
                    }
                    else if (item.didian5 == " ")
                    {
                        item.didian5 = "";
                    }
                }
                DgCurriculum.ItemsSource = null;
                DgCurriculum.ItemsSource = list;
            }
        }
    }
}
